package com.fa.wisdom.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.coder.zzq.smartshow.dialog.LoadingDialog;
import com.fa.wisdom.R;
import com.fa.wisdom.adapter.FoundRecycleAdapter;
import com.fa.wisdom.base.BaseFragment;
import com.fa.wisdom.base.Constant;
import com.fa.wisdom.base.LazyLoadBaseFragment;
import com.fa.wisdom.model.MsgEntity;
import com.fa.wisdom.model.ResultEntity;
import com.fa.wisdom.net.MyCallback;
import com.fa.wisdom.net.Task;
import com.fa.wisdom.ui.JKActivity;
import com.fa.wisdom.utils.Loading;
import com.fa.wisdom.utils.ScreenUtil;
import com.fa.wisdom.utils.SpUtils;
import com.tianyi.putorefresh.BaseBothListener;
import com.tianyi.putorefresh.PullToBothLayout;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Response;


public class MsgFragment extends BaseFragment {
    RecyclerView recyclerView;
    View statusBarPlace;
    PullToBothLayout pullToRefresh_layout;
    private FoundRecycleAdapter foundRecycleAdapter = null;
    List<MsgEntity> msgEntities = new ArrayList<>();
    private int page = 1;
    private int id;
    private boolean isRefresh = false;

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        recyclerView = view.findViewById(R.id.recycle);
        pullToRefresh_layout = view.findViewById(R.id.pullRefresh);
        statusBarPlace = view.findViewById(R.id.view_status_bar_place);
        ViewGroup.LayoutParams params = statusBarPlace.getLayoutParams();
        params.height = ScreenUtil.getStatusBarHeight(getContext());
        statusBarPlace.setLayoutParams(params);
        initAdapter();

        setListen();
    }


    private void initAdapter() {
        foundRecycleAdapter = new FoundRecycleAdapter(msgEntities);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setNestedScrollingEnabled(false);
        recyclerView.setAdapter(foundRecycleAdapter);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            isRefresh=true;
            page = 1;
            id = SpUtils.getInstance().decodeInt("group_id", 1);
            loadData();
        }
    }

    private void setListen() {

        pullToRefresh_layout.setRefreshListener(new BaseBothListener() {
            @Override
            public void refresh() {
                page = 1;
                loadData();
                isRefresh = true;
                pullToRefresh_layout.finishRefresh();
            }

            @Override
            public void loadMore() {
                page++;
                isRefresh = false;
                loadData();
                pullToRefresh_layout.finishLoadMore();
            }
        });
    }


    //加载数据
    private void loadData() {
        LoadingDialog dialog = Loading.getInstance().onShowLargeLoading("加载中...");
        String token = SpUtils.getInstance().decodeString(Constant.TOKEN, "");
        dialog.showInActivity(requireActivity());
        Task.getApiService().getMsgList(id, page, token).enqueue(new MyCallback<ResultEntity>() {
            @Override
            protected void onSuccess(Response<ResultEntity> response) {
                dialog.dismiss();
                pullToRefresh_layout.finishLoadMore();
                pullToRefresh_layout.finishRefresh();
                String jsonString = JSON.toJSONString(response.body());
                JSONObject jsonObject = JSON.parseObject(jsonString);
                JSONArray jsonArray = jsonObject.getJSONArray("data");
                if (jsonArray != null && jsonArray.size() > 0) {
                    if (isRefresh)
                        msgEntities.clear();
                    for (int i = 0; i < jsonArray.size(); i++) {
                        JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                        MsgEntity msgEntity = new MsgEntity();
                        msgEntity.setContent(jsonObject1.getString("alarmcode"));
                        msgEntity.setTime(jsonObject1.getString("create_time"));
                        msgEntity.setTitle(jsonObject1.getString("devicetype"));
                        msgEntities.add(msgEntity);
                    }
                }
                foundRecycleAdapter.notifyDataSetChanged();
            }

            @Override
            protected void onFailure(Throwable t) {
                dialog.dismiss();
                pullToRefresh_layout.finishLoadMore();
                pullToRefresh_layout.finishRefresh();
            }
        });


    }

    @Override
    public View setContentView(LayoutInflater inflater) {
        return inflater.inflate(R.layout.fragment_msg, null);
    }
}
